import java.util.Scanner;

public class MyPi {
    int total;  //统计所有点的个数
    int sum;    //统计在圆内的点的数量
    double x, y;//标记点的坐标

    /**
     * 估算PI的值
     * @param args
     */
    private double Pi(int total) {
        for (int i = 0; i < total; i++) {
            x = Math.random();
            y = Math.random();
            if (Math.sqrt(x*x + y*y) < 1)
                sum++;
        }
        return 4.*sum/total;
    }

    public static void main(String[] args) {
        System.out.println("用Monte Carlo方法求π的近似值：");
        System.out.println("输入一个数字，尽量大：");

        MyPi myPi = new MyPi();

        Scanner scanner = new Scanner(System.in);
        myPi.total = scanner.nextInt();

        System.out.printf("PI的值约为： " + myPi.Pi(myPi.total));
    }
}
